package cn.e4j.modular.api.service;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.api.mapper.PjjsMapper;
import cn.e4j.modular.api.util.CommonUtils;
import cn.e4j.modular.crm.entity.GwaaT;
import cn.e4j.modular.crm.entity.MmruT;
import cn.e4j.modular.system.entity.SysUser;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * 寄件申请
 */
@Service
public class PjjsService {
    @Autowired
    private PjjsMapper pjjsMapper;

    /**
     * 获取配件列表
     *
     * @param inDto
     * @return
     */
    public List<Dto> getPartListData(Page<Object>page,Dto inDto) {
        return pjjsMapper.getPartListPage(page,inDto);
    }

    /**
     * 获取维修单号和服务客户
     *
     * @param inDto
     * @return
     */
    public List<Dto> getMmrtData(Page<Object>page,Dto inDto) {

        return pjjsMapper.getmmrtPage(page,inDto);
    }

    /**
     * 配件寄送提交
     *
     * @param inDto
     */
    @Transactional
    public void savePjjs(SysUser user,Dto inDto) {
        Date date = new Date();
        JSONArray list = CommonUtils.toJSONArray(inDto.getString("list"));
        List<MmruT> orderList = list.toJavaList(MmruT.class);
        String mmrfdocno = "PJJS" + DateUtils.getDate("yyyyMMddHHmmssSSS");
        for (int i = 0; i < orderList.size(); i++) {
            MmruT mmruT = orderList.get(i);
            JSONObject jsonObj = list.getJSONObject(i);
            mmruT.setMMRUENT(user.getDBC());
            mmruT.setMMRUDOCNO(mmrfdocno);
            mmruT.setMMRUSEQ(BigDecimal.valueOf(i + 1));
            //mmruT.setMMRUDOCDATE(DateUtils.parseDate(DateUtils.formatDate(date, DateUtils.PATTERN_yyyy_MM_dd)));
            mmruT.setMMRUDOCDATE(LocalDate.now());
            mmruT.setMMRU001(jsonObj.getString("MMRTDOCNO"));
            mmruT.setMMRU002(BigDecimal.valueOf(i + 1));
            mmruT.setMMRU003(jsonObj.getString("MMRT003"));
            mmruT.setMMRU004(jsonObj.getString("MMRT004"));
            GwaaT gwaaT = new GwaaT().selectOne(new QueryWrapper<GwaaT>().lambda().eq(GwaaT::getGWAAENT,user.getDBC())
                    .eq(GwaaT::getGWAA001,user.getACCOUNT()
                    ));
            mmruT.setMMRU006(gwaaT.getGWAA002());
            mmruT.setMMRU007(gwaaT.getGWAA004());
            mmruT.setMMRUACTI("Y");
            mmruT.setMMRUUSER(user.getID());
            mmruT.setMMRUGRUP(user.getORG_ID());
            mmruT.setMMRUORIU(user.getID());
            mmruT.setMMRUORIG(user.getORG_ID());
            //mmruT.setMMRUDATE(date);
            mmruT.setMMRUDATE(LocalDate.now());
            mmruT.setMMRUTIME(DateUtils.formatDate(date, DateUtils.PATTERN_HH_mm_ss));
            mmruT.insert();
        }
    }

}
